import { Watermark } from "@pansy/watermark"
import { nextTick } from "vue"
import { useWatermarkStore } from "@/store/useWatermarkStore"

export function useWatermark() {
  const watermarkStore = useWatermarkStore()
  const defaultOptions = {
    text: "andmin@147", // 用户账号，从longin-store获取
    opacity: 0.15,
  }
  const watermark = function () {
    console.log(111)
  }

  watermark.set = (options: any = {}): void => {
    //只能创建一次~~
    if (!watermarkStore.watermark.container) {
      nextTick(() => {
        watermarkStore.watermark = new Watermark(Object.assign(defaultOptions, options))
      })
    } else {
      watermark.show()
    }
  }

  watermark.show = (): void => {
    watermarkStore.watermark?.show?.()
  }

  watermark.hide = (): void => {
    watermarkStore.watermark?.hide()
  }

  watermark.update = (options: any = {}): void => {
    // 更新只能对样式更新，不能对容器对象进行module更新~~
    watermarkStore.watermark?.update(Object.assign(defaultOptions, options))
  }

  watermark.destory = (): void => {
    watermarkStore.watermark?.destroy?.()
    // 在销毁水印对象destoryWatermark 成功之后，要将局部变量watermark重置为null
    watermarkStore.watermark = {}
  }

  return {
    watermark,
  }
}
